/*
Author : Nguyen Van Ty
Email  : ngvanty03@gmail.com
Date   : 1/17/2011 

*/

using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using BusinessObject;
using Utility;
using System.Xml;
namespace DataAccessLayer
{
    /// <summary>
    /// Define DMXeDaoBase class. This class user for DataAccessLayer, contain 4 function Insert, Update, Search, Delete.
    /// If you want add new function, you must define in DMXeDao class , not here.
    /// </summary>
    public class DMXeDaoBase
    {
        private static ILog logger = null;
        #region Constructor
        /// <summary>
        /// Define Constructor.
        /// </summary>
        public DMXeDaoBase() { }
        static DMXeDaoBase()
        {
            //Init logger.
            logger = LoggingUtility.GetLogger(typeof(DMXeDaoBase).FullName);
        }
        #endregion
        /// <summary>
        /// Read data from IDataReader, convert to DMXe Object.
        /// </summary>
        private static DMXe GetDMXeByDataReader(IDataReader reader)
        {
            String methodName = "GetDMXeByDataReader";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                DMXe result = new DMXe();
                //read MaXe field data.

                result.MaXe = ManagerDao.GetintDataFromDataReader(reader, "MaXe");

                //read TenXe field data.

                result.TenXe = ManagerDao.GetstringDataFromDataReader(reader, "TenXe");

                //read ChungXe field data.

                result.ChungXe = ManagerDao.GetintDataFromDataReader(reader, "ChungXe");

                //read IsPublished field data.

                result.IsPublished = ManagerDao.GetbooleanDataFromDataReader(reader, "IsPublished");

                //read Order field data.

                result.Order = ManagerDao.GetintDataFromDataReader(reader, "Order");

                //read CreatedBy field data.

                result.CreatedBy = ManagerDao.GetstringDataFromDataReader(reader, "CreatedBy");

                //read CreatedDate field data.

                result.CreatedDate = ManagerDao.GetdatetimeDataFromDataReader(reader, "CreatedDate");

                //read LastUpdatedBy field data.

                result.LastUpdatedBy = ManagerDao.GetstringDataFromDataReader(reader, "LastUpdatedBy");

                //read LastUpdatedDate field data.

                result.LastUpdatedDate = ManagerDao.GetdatetimeDataFromDataReader(reader, "LastUpdatedDate");

                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }
        /// <summary>
        /// Define function Insert for DMXe Object.
        /// </summary>
        /// <param name="obj">Object Insert.</param>
        /// <returns>
        ///    True: Successfull.
        ///    False: Process Error.
        ///</returns>



        /// <summary>
        /// Get Data DMXe object. 
        /// </summary>
        /// <param name="maXe" >This is primary key to get data.</param>
        /// <returns>DMXe object.</returns>

        public static DMXe Get(int maXe)
        {
            String methodName = "GetDMXe";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                DMXe result = null;
                DbCommand dbCommnand = ManagerDao.GetStoredProcCommand("spGetDMXe");
                //Add value "MaXe"  paramater.
                ManagerDao.AddInParameter(dbCommnand, "MaXe", DbType.Int32, maXe);
                IDataReader reader = ManagerDao.ExecuteReader(dbCommnand);
                if (reader.Read())
                {
                    result = GetDMXeByDataReader(reader);

                }
                reader.Close();
                return result;
            }
            catch (System.Exception e)
            {
                //write error message to log file.
                logger.Error("Get", e);
                return null;
            }
        }
        /// <summary>
        /// Get All DMXe object from table DMXe.
        /// </summary>
        /// <returns>List DMXe object.</returns>
        public static List<DMXeResult> GetAll()
        {
            String methodName = "GetAll";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                List<DMXeResult> listDMXe = new List<DMXeResult>();
                DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spGetAllDMXe");

                using (IDataReader reader = ManagerDao.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        listDMXe.Add(GetDMXeResultByDataReader(reader));
                    }
                    reader.Close();
                }

                return listDMXe;
            }
            catch (Exception ex)
            {
                //write error message to log file.
                logger.Error(methodName, ex);
                return null;
            }
        }

        /// <summary>
        /// Search DMXe object in table DMXe with condition is t object.
        /// </summary>
        /// <param name="t">Condition for Search.</param>       
        /// <param name="totalRow">  total records in this table.</param>
        /// <returns>List DMXeResult object.</returns>
        public static List<DMXeResult> SearchDMXeSomeCondition(DMXe t)
        {
            String methodName = "SearchDMXeSomeCondition";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                List<DMXeResult> listDMXe = new List<DMXeResult>();
                DbCommand cmd = ManagerDao.GetStoredProcCommand("spSearchDMXeByCondition");
                //Add value Condition  paramater.
                ManagerDao.AddInParameter(cmd, "Condition", DbType.String, ConvertValueObject.CastObjValueToInsertDB(t.Condition));

                //Add value PageIndex  paramater.
                ManagerDao.AddInParameter(cmd, "PageIndex", DbType.Int32, t.ObjectPaging.PageIndex);
                //Add value PageSize  paramater.
                ManagerDao.AddInParameter(cmd, "PageSize", DbType.Int32, t.ObjectPaging.PageSize);
                //Add value SortField  paramater.
                ManagerDao.AddInParameter(cmd, "SortField", DbType.String, t.ObjectSortOrder.SortField);
                //Add value SortExpression  paramater.
                ManagerDao.AddInParameter(cmd, "SortExpression", DbType.String, t.ObjectSortOrder.SortExpression);
                using (IDataReader reader = ManagerDao.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        listDMXe.Add(GetDMXeResultByDataReader(reader));
                        if (t.ToltalRow <= 0)
                            t.ToltalRow = ManagerDao.Getint32DataFromDataReader(reader, "TotalRow");
                    }

                    reader.Close();
                }

                return listDMXe;
            }
            catch (Exception ex)
            {
                //write error message to log file.
                logger.Error(methodName, ex);
                return null;
            }
        }
        /// <summary>
        /// Delete DMXe object. 
        /// </summary>
        /// <param name="MaXe" >This is primary key to delete.</param>
        /// <returns>True/False.</returns>

        public static bool Delete(int MaXe)
        {
            String methodName = "DeleteDMXe";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spDeleteDMXe");
                //Add value "MaXe"  paramater.
                ManagerDao.AddInParameter(dbCommand, "MaXe", DbType.Int32, MaXe);
                //Add value returnValue  paramater.
                ManagerDao.AddParameter(dbCommand, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
                try
                {
                    ManagerDao.ExecuteNonQuery(dbCommand);
                    int parameterValue = (int)ManagerDao.GetParameterValue(dbCommand, "returnValue");
                    if (parameterValue == 1)
                    {
                        return true;
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                return false;
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                return false;
            }
        }
        /// <summary>
        /// Read data from IDataReader, convert to DMXeResult Object.
        /// </summary>
        private static DMXeResult GetDMXeResultByDataReaderByLanguage(IDataReader reader, string LanguageId)
        {
            String methodName = "GetDMXeResultByDataReader";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                DMXeResult result = new DMXeResult();
                //read MaXe field data.
                result.MaXe = ManagerDao.GetintDataFromDataReader(reader, "MaXe");

                //read TenXe field data.
                result.TenXe = ManagerDao.GetstringDataFromDataReader(reader, "TenXe");

                //read ChungXe field data.
                result.ChungXe = ManagerDao.GetintDataFromDataReader(reader, "ChungXe");

                //read IsPublished field data.
                result.IsPublished = ManagerDao.GetbooleanDataFromDataReader(reader, "IsPublished");

                //read Order field data.
                result.Order = ManagerDao.GetintDataFromDataReader(reader, "Order");

                //read CreatedBy field data.
                result.CreatedBy = ManagerDao.GetstringDataFromDataReader(reader, "CreatedBy");

                //read CreatedDate field data.
                result.CreatedDate = ManagerDao.GetdatetimeDataFromDataReader(reader, "CreatedDate");

                //read LastUpdatedBy field data.
                result.LastUpdatedBy = ManagerDao.GetstringDataFromDataReader(reader, "LastUpdatedBy");

                //read LastUpdatedDate field data.
                result.LastUpdatedDate = ManagerDao.GetdatetimeDataFromDataReader(reader, "LastUpdatedDate");

                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }
        /// <summary>
        /// Read data from IDataReader, convert to DMXeResult Object.
        /// </summary>
        private static DMXeResult GetDMXeResultByDataReader(IDataReader reader)
        {
            String methodName = "GetDMXeResultByDataReader";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                DMXeResult result = new DMXeResult();
                //read MaXe field data.
                result.MaXe = ManagerDao.GetintDataFromDataReader(reader, "MaXe");

                //read TenXe field data.
                result.TenXe = ManagerDao.GetstringDataFromDataReader(reader, "TenXe");

                //read ChungXe field data.
                result.ChungXe = ManagerDao.GetintDataFromDataReader(reader, "ChungXe");

                //read IsPublished field data.
                result.IsPublished = ManagerDao.GetbooleanDataFromDataReader(reader, "IsPublished");

                //read Order field data.
                result.Order = ManagerDao.GetintDataFromDataReader(reader, "Order");

                //read CreatedBy field data.
                result.CreatedBy = ManagerDao.GetstringDataFromDataReader(reader, "CreatedBy");

                //read CreatedDate field data.
                result.CreatedDate = ManagerDao.GetdatetimeDataFromDataReader(reader, "CreatedDate");

                //read LastUpdatedBy field data.
                result.LastUpdatedBy = ManagerDao.GetstringDataFromDataReader(reader, "LastUpdatedBy");

                //read LastUpdatedDate field data.
                result.LastUpdatedDate = ManagerDao.GetdatetimeDataFromDataReader(reader, "LastUpdatedDate");

                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }
        public static List<DMXeResult> GetByChungXe(int chungXe)
        {
            String methodName = "GetByChungXe";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                List<DMXeResult> listDMXe = new List<DMXeResult>();
                DbCommand cmd = ManagerDao.GetStoredProcCommand("spGetByChungXe");


                //Add value PageIndex  paramater.
                ManagerDao.AddInParameter(cmd, "ChungXe", DbType.Int32, chungXe);

                using (IDataReader reader = ManagerDao.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        listDMXe.Add(GetDMXeResultByDataReader(reader));

                    }

                    reader.Close();
                }

                return listDMXe;
            }
            catch (Exception ex)
            {
                //write error message to log file.
                logger.Error(methodName, ex);
                return null;
            }
        }


    }
}